package com.study.hystrix;

import rx.Observable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class HystrixCommandDemoTest {

    public static void testSynchronous() {
        System.out.println(new HystrixCommandDemo("World").execute());
    }

    public static void testAsynchronous() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Future<String> fWorld = new HystrixCommandDemo("World").queue();
                try {
                    System.out.println(fWorld.get());  //异步步执行用get()来获取结果
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public static void testObserve() {
        Observable<String> ho = new HystrixCommandDemo("World").observe();
        System.out.println(ho.toBlocking().single());
    }

    public static void testToObservable() {
        Observable<String> co = new HystrixCommandDemo("World").toObservable();
        System.out.println(co.toBlocking().single());
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //testSynchronous();
        testAsynchronous();
        //testObserve();
        //testToObservable();
    }
}
